/*
 * misc.c
 *
 *  Created on: 03/05/2013
 *      Author: jose
 */


#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "misc.h"

String *
string_create_no_len(const char * str)
{
	assert(str);

	String * s;

	s = (String *) calloc(1, sizeof(String));

	s->len = strlen(str);

	assert(s->len > 0);

	s->text = (char *) calloc((s->len), sizeof(char));
	memcpy(s->text, str, sizeof(char) * s->len );

	return s;
}


String *
string_create_with_len(const char * str, const unsigned len)
{
	assert(str);
	assert(len > 0);

	String * s;

	s = (String *) calloc(1, sizeof(String));

	s->len = len;

	s->text = (char *) calloc((s->len), sizeof(char));
	memcpy(s->text, str, sizeof(char) * s->len );

	return s;
}

/*
 * Return:
 * 	0 if equals
 * 	!=0 otherwise
 */
short int
string_compare(String * s1, String * s2)
{
	assert(s1);
	assert(s2);

	if ( s1->len == s2->len && !memcmp(s1->text, s2->text, s1->len) )
		return 0;

	return 1;
}

